package com.ruoyi.youtobe.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.until.CheckUtilsImp;
import com.ruoyi.youtobe.domain.YoutobeVideoLable;
import com.ruoyi.youtobe.mapper.YoutobeChannelVideoMapper;
import com.ruoyi.youtobe.mapper.YoutobeVideoLableMapper;
import com.ruoyi.youtobe.service.IYoutobeVideoLableService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Youtobe视频标签Service业务层处理
 *
 * @author ruoyi
 * @date 2025-11-16
 */
@Service
public class YoutobeVideoLableServiceImpl implements IYoutobeVideoLableService {
    @Resource
    private YoutobeVideoLableMapper youtobeVideoLableMapper;

    /**
     * 查询Youtobe视频标签
     *
     * @param id Youtobe视频标签主键
     * @return Youtobe视频标签
     */
    @Override
    public YoutobeVideoLable selectYoutobeVideoLableById(Long id)
    {
        return youtobeVideoLableMapper.selectYoutobeVideoLableById(id);
    }

    /**
     * 查询Youtobe视频标签列表
     * 
     * @param youtobeVideoLable Youtobe视频标签
     * @return Youtobe视频标签
     */
    @Override
    public List<YoutobeVideoLable> selectYoutobeVideoLableList(YoutobeVideoLable youtobeVideoLable)
    {
        return youtobeVideoLableMapper.selectYoutobeVideoLableList(youtobeVideoLable);
    }

    /**
     * 新增Youtobe视频标签
     * 
     * @param youtobeVideoLable Youtobe视频标签
     * @return 结果
     */
    @Override
    public int insertYoutobeVideoLable(YoutobeVideoLable youtobeVideoLable)
    {
        youtobeVideoLable.setCreateTime(DateUtils.getNowDate());
        return youtobeVideoLableMapper.insertYoutobeVideoLable(youtobeVideoLable);
    }

    /**
     * 修改Youtobe视频标签
     * 
     * @param youtobeVideoLable Youtobe视频标签
     * @return 结果
     */
    @Override
    public int updateYoutobeVideoLable(YoutobeVideoLable youtobeVideoLable)
    {
        youtobeVideoLable.setUpdateTime(DateUtils.getNowDate());
        return youtobeVideoLableMapper.updateYoutobeVideoLable(youtobeVideoLable);
    }

    /**
     * 批量删除Youtobe视频标签
     * 
     * @param ids 需要删除的Youtobe视频标签主键
     * @return 结果
     */
    @Override
    public int deleteYoutobeVideoLableByIds(Long[] ids)
    {
        return youtobeVideoLableMapper.deleteYoutobeVideoLableByIds(ids);
    }

    /**
     * 删除Youtobe视频标签信息
     *
     * @param id Youtobe视频标签主键
     * @return 结果
     */
    @Override
    public int deleteYoutobeVideoLableById(Long id) {
        return youtobeVideoLableMapper.deleteYoutobeVideoLableById(id);
    }

    @Resource
    YoutobeChannelVideoMapper youtobeChannelVideoMapper;

    @Override
    public int insertYoutobeVideoAllLable() {
        //1.女频数据获取
        String wo = getLable("女");
        YoutobeVideoLable inwomen = new YoutobeVideoLable();
        inwomen.setTiKey("女频全标题");
        inwomen.setTiValue(wo);
        inwomen.setType("女");
        int i = youtobeVideoLableMapper.insertYoutobeVideoLable(inwomen);
        String man = getLable("男");
        YoutobeVideoLable inman = new YoutobeVideoLable();
        inman.setTiKey("男频全标题");
        inman.setTiValue(man);
        inman.setType("男");
        int i1 = youtobeVideoLableMapper.insertYoutobeVideoLable(inman);
        return i + i1;
    }

    String getLable(String type) {
        List<String> allList = youtobeChannelVideoMapper.selectVideoKeyListByType(type);
        if (CheckUtilsImp.listIsNotEmpty(allList)) {
            List<String> list = new ArrayList<>(16);
            StringBuffer sb = new StringBuffer();
            for (String la : allList) {
                String[] split = la.split("#");
                for (String s : split) {
                    if (!list.contains(s) && s.length() <= 5) {
                        list.add(s);
                        sb.append("  #").append(s);
                    }
                }
            }
            return sb.toString();

        }
        return "";
    }


}
